<?php

namespace app\controller\admin\sky;

use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Strategy
 * @package app\controller\admin\sky
 */
class Position extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法


    public function _initialize()
    {
        $this->model = new \app\model\api\SkySeasonCandle();
    }

    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            if(empty($post['season'])){
                throw new \Exception('请选择季节');
            }

            $saveData = [
                'season'  => $post['season'],
                'area1'   => $post['area1'],
                'area2'   => $post['area2'],
                'area3'   => $post['area3'],
                'area4'   => $post['area4'],
                'area5'   => $post['area5'],
                'area1_content1'   => json_encode(explode(',',$post['area1_content1']),JSON_UNESCAPED_UNICODE),
                'area2_content1'   => json_encode(explode(',',$post['area2_content1']),JSON_UNESCAPED_UNICODE),
                'area3_content1'   => json_encode(explode(',',$post['area3_content1']),JSON_UNESCAPED_UNICODE),
                'area4_content1'   => json_encode(explode(',',$post['area4_content1']),JSON_UNESCAPED_UNICODE),
                'area5_content1'   => json_encode(explode(',',$post['area5_content1']),JSON_UNESCAPED_UNICODE),
                'area1_content2'   => json_encode(explode(',',$post['area1_content2']),JSON_UNESCAPED_UNICODE),
                'area2_content2'   => json_encode(explode(',',$post['area2_content2']),JSON_UNESCAPED_UNICODE),
                'area3_content2'   => json_encode(explode(',',$post['area3_content2']),JSON_UNESCAPED_UNICODE),
                'area4_content2'   => json_encode(explode(',',$post['area4_content2']),JSON_UNESCAPED_UNICODE),
                'area5_content2'   => json_encode(explode(',',$post['area5_content2']),JSON_UNESCAPED_UNICODE),

            ];
            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        if(empty($post['season'])){
            throw new \Exception('请选择日期');
        }

        $row->season      = $post['season'];
        $row->area1   = $post['area1'];
        $row->area2   = $post['area2'];
        $row->area3   = $post['area3'];
        $row->area4   = $post['area4'];
        $row->area5   = $post['area5'];
        $row->area1_content1   = json_encode(explode(',',$post['area1_content1']),JSON_UNESCAPED_UNICODE);
        $row->area2_content1   = json_encode(explode(',',$post['area2_content1']),JSON_UNESCAPED_UNICODE);
        $row->area3_content1   = json_encode(explode(',',$post['area3_content1']),JSON_UNESCAPED_UNICODE);
        $row->area4_content1   = json_encode(explode(',',$post['area4_content1']),JSON_UNESCAPED_UNICODE);
        $row->area5_content1   = json_encode(explode(',',$post['area5_content1']),JSON_UNESCAPED_UNICODE);
        $row->area1_content2   = json_encode(explode(',',$post['area1_content2']),JSON_UNESCAPED_UNICODE);
        $row->area2_content2   = json_encode(explode(',',$post['area2_content2']),JSON_UNESCAPED_UNICODE);
        $row->area3_content2   = json_encode(explode(',',$post['area3_content2']),JSON_UNESCAPED_UNICODE);
        $row->area4_content2   = json_encode(explode(',',$post['area4_content2']),JSON_UNESCAPED_UNICODE);
        $row->area5_content2   = json_encode(explode(',',$post['area5_content2']),JSON_UNESCAPED_UNICODE);

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');

        $info = $this->model->where('id',$id)->find()->toArray();
        $info['area1_content1'] = json_decode($info['area1_content1']) ?? [];
        $info['area2_content1'] = json_decode($info['area2_content1']) ?? [];
        $info['area3_content1'] = json_decode($info['area3_content1']) ?? [];
        $info['area4_content1'] = json_decode($info['area4_content1']) ?? [];
        $info['area5_content1'] = json_decode($info['area5_content1']) ?? [];
        $info['area1_content2'] = json_decode($info['area1_content2']) ?? [];
        $info['area2_content2'] = json_decode($info['area2_content2']) ?? [];
        $info['area3_content2'] = json_decode($info['area3_content2']) ?? [];
        $info['area4_content2'] = json_decode($info['area4_content2']) ?? [];
        $info['area5_content2'] = json_decode($info['area5_content2']) ?? [];

        return $this->success('获取成功', $info);
    }
}